/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.stripesbook.rayures.view.stripes;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.exception.StripesJspException;
import net.sourceforge.stripes.localization.LocalizationUtility;
import net.sourceforge.stripes.tag.FormTag;

/**
 * Corresponds to {@link FormTag} and {@code <s:form>}.
 *
 * @author Frederic Daoud
 */
public class SForm extends STagSupport<FormTag, SForm> {

  public SForm() {
    super(new FormTag());
  }

  /**
   * Sets {@code beanclass} to the current action bean.
   */
  @Override
  public void onInit() {
    super.onInit();
    beanclass(viewContext.getActionBean().getClass());
  }

  public SForm action(String action) {
    getTag().setAction(action);
    return getThis();
  }

  public SForm beanclass(Class<? extends ActionBean> beanclass) {
    try {
      getTag().setBeanclass(beanclass);
      return getThis();
    }
    catch (StripesJspException exc) {
      throw new RuntimeException("Could not set the beanclass to " + beanclass, exc);
    }
  }

  public SForm accept(String accept) {
    getTag().setAccept(accept);
    return getThis();
  }

  public SForm acceptcharset(String acceptcharset) {
    getTag().setAcceptcharset(acceptcharset);
    return getThis();
  }

  public SForm enctype(String enctype) {
    getTag().setEnctype(enctype);
    return getThis();
  }

  public SForm focus(String focus) {
    getTag().setFocus(focus);
    return getThis();
  }

  public SForm method(String method) {
    getTag().setMethod(method);
    return getThis();
  }

  public SForm name(String name) {
    getTag().setName(name);
    return getThis();
  }

  public SForm onreset(String onreset) {
    getTag().setOnreset(onreset);
    return getThis();
  }

  public SForm onsubmit(String onsubmit) {
    getTag().setOnsubmit(onsubmit);
    return getThis();
  }

  public SForm partial(boolean partial) {
    getTag().setPartial(partial);
    return getThis();
  }
  public boolean isPartial() {
    return getTag().isPartial();
  }

  public SForm target(String target) {
    getTag().setTarget(target);
    return getThis();
  }

  protected String getLabel(String field) {
    String label = bundle.get(field);
    if (label != null) {
      return label;
    }
    return LocalizationUtility.makePseudoFriendlyName(field);
  }
}
